/***************************************************************************
 *
 * This document contains confidential and proprietary information 
 * subject to non-disclosure agreements with AsiaInspection. This 
 * information shall not be distributed or copied without written 
 * permission from the AsiaInspection.
 *
 ***************************************************************************/
package concurrent;

import java.util.concurrent.Callable;

/***************************************************************************
 *<PRE>
 *  Project Name    : com.allen
 *
 *  Package Name    : concurrent
 *
 *  File Name       : DownloadMovieTask.java
 *
 *  Creation Date   : Dec 25, 2018
 *
 *  Author          : Allen Zhang
 *
 *  Purpose         : 模仿下载一部电影, 随机文件大小
 *
 *
 *  History         : TODO
 *
 *</PRE>
 ***************************************************************************/

public class DownloadMovieTask implements Callable<Integer> {

    private int size;

    private String name;

    public int getSize() {
        return size;
    }

    public String getName() {
        return name;
    }

    DownloadMovieTask(String name) {
        this.name = name;
        //随机文件大小
        this.size= (int) (Math.random() * 15000);
    }

    //download movie and return size
    @Override
    public Integer call() throws Exception {
        int downloaded = 0;
        System.out.println("开始下载电影: " + name + ", size: " + size + " MB.");
        while (downloaded < size) {
            Thread.sleep(500);
            if (downloaded != 0) {
                System.out.println(name + " 已下载" + downloaded + " MB.");
            }
            downloaded += 220;  //下载速度设慢点就可以看到池里可用的线程都在工作
        }
        System.out.println("-------------------" + name + "下载完毕, " + size + " MB.");
        return size;
    }

}
